<?php
namespace app\admin\controller;
use x\my\Umeng;
class Company extends Base
{

    //列表
    public function lists(){
        if(is_post()){
            $d = $this->post;
            $map = [];
            if($d['keyword']!='') $map[] = ['and','title|phone|username','like',"%{$d['keyword']}%"];
            if($d['status']!='') $map[] = ['and','status','=',$d['status']];
            if($d['min_time']!='') $map[] = ['and','create_time','>=',strtotime($d['min_time'])];
            if($d['max_time']!='') $map[] = ['and','create_time','<=',strtotime($d['max_time'])];
            $w  = where($map);
            $sql = "
                SELECT
                    id,
                    title,
                    phone,
                    username,
                    address,
                    status,
                    sort,
                    create_time
                FROM
                    company
                    $w[0]
                ORDER BY
                    id
                DESC
            ";
            $rs = $this->db->query($sql.limit($d['page'],$d['limit']), $w[1],'',true);
            if($rs){
                $rule = [
                    'status' => function($v){return c("type.status.{$v}");},
                    'is_rz' => function($v){return c("type.is_rz.{$v}");},
                    'create_time' => 'com_time_format',
                ];
                $data = com_format($rs[0],$rule);
                suc($data,$rs[1]);
            }else{
                suc([],0);
            }
        }
        $this->fetch();
    }


    //订单列表
    public function company_lists(){
        if(is_post()){
            $d = $this->post;
            $sql = "
                SELECT
                    *
                FROM
                    worker
                WHERE
                    company_ids like ? 
            ";
            $rs = $this->db->query($sql.limit($d['page'],$d['limit']), [ '%,'.$d['id'].',%' ] ,'',true);
            
            if($rs){
                $rule = [
                    'status' => function($v){return c("type.status.{$v}");},
                    'sex' => function($v){return c("type.sex.{$v}");},
                    'create_time' => 'com_time_format',
                ];
                $data = com_format($rs[0],$rule);
                suc($data,$rs[1]);
            }else{
                suc([],0);
            }
        }
    }

    //添加
    public function add(){
        if(is_post()){
            $d = $this->post;
            $rule = [
                'title'=>'require',
            ];
            $msg = [
                'title.require' => '公司名称不能为空',
            ];
            check($rule,$msg,$d);
            $d['create_time'] = time();
            $id = m('Company','c')->add($d);
            if ($id) {
                cookie('js_msg','添加成功');
                cookie('js_function','preload');
                return suc();
            }else{
                err('添加失败');
            }
        }
        return $this->fetch();
    }

    //编辑
    public function edit(){
        if(is_post()){
            $d = $this->post;
            $rule = [
                'title'=>'require',
            ];
            $msg = [
                'title.require' => '公司名称不能为空',
            ];
            check($rule,$msg,$d);
            $this->db->update("company",$d);
            cookie('js_msg','修改成功');
            cookie('js_function','preload');
            return suc();
        }
        $id = $this->get["id"];
        $sql = "SELECT * FROM company WHERE id=?";
        $info = $this->db->query($sql,[$id],1);
        $this->fetch(["info" => $info]);
    }

    //删除
    public function del(){
        if(is_post()){
            $d = $this->post;
            $this->db->delete('company',$d['id']);
            cookie('js_msg','删除成功');
            cookie('js_function','close|reload');
            return suc();
        }
    }

    //公司详情
    public function detail(){
        $id = $this->get("id");
        $info = $this->db->query("select * from company where id = ?",[$id],1);
        $this->fetch(['info'=>$info]);
    }
}
